package com.security.manage.user.controller;

import com.security.manage.user.dto.ComboDto;
import com.security.manage.user.model.Permission;
import com.security.manage.user.model.ResData;
import com.security.manage.user.repository.PermissionRepository;
import io.swagger.annotations.Api;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@Api("权限管理")
public class PermissionController {

    @Resource
    private PermissionRepository permissionRepository;

    @PostMapping("/perm")
    public ResData addPermission(@RequestBody Permission permission){
        permissionRepository.save(permission);
        return new ResData(1,"成功");
    }

    @DeleteMapping("/perm/{id}")
    public ResData delPermission(@PathVariable("id") Long id){
        permissionRepository.deleteById(id);
        return new ResData(1,"成功");
    }

    @GetMapping("/perm/{id}")
    public ResData getPermission(@PathVariable("id") Long id){
        Optional<Permission> optionalDept = permissionRepository.findById(id);
        return optionalDept.map(dept -> new ResData<>(1, "获取成功", dept)).orElseGet(() -> new ResData(0, "获取失败"));
    }

    @GetMapping("/perm/all")
    public ResData permCombo(){

        List<Permission> permissions = permissionRepository.findAll();
        List<Permission> menus = permissions.stream().filter(p -> p.getPid() == -1L).collect(Collectors.toList());
        List<Map<String,Object>> result = new ArrayList<>();
        for (Permission p : menus){
            Long id = p.getId();
            Map<String,Object> map = new HashMap<>();
            map.put("id",p.getId());
            map.put("name",p.getPermName());
            List<Permission> subList = permissions.stream().filter(p1 -> p1.getPid() == id).collect(Collectors.toList());
            List<Map<String,Object>> dtos = new ArrayList<>();
            for (Permission permission:subList) {
                Map<String,Object> map1 =new HashMap<>();
                map1.put("id",permission.getId());
                map1.put("name",permission.getPermName());
                map1.put("subList",new ArrayList<>());
                dtos.add(map1);
            }
            map.put("subList",dtos);
            result.add(map);
        }

        return  new ResData(1,"获取数据成功",result);
    }

    @GetMapping("/perm/list")
    public ResData listPermission(@RequestParam(value = "page",defaultValue = "0") Integer page,
                           @RequestParam(value = "size",defaultValue = "10") Integer size){

        Sort sort = Sort.by(Sort.Direction.DESC, "id");
        PageRequest of = PageRequest.of(page, size, sort);
        Page<Permission> pages = permissionRepository.findAll(of);

        Map<String,Object> map = new HashMap<>();
        map.put("total",pages.getTotalElements());
        List<Permission> collect = pages.get().collect(Collectors.toList());
        map.put("data",collect);

        return new ResData<>(1, "获取成功", map);
    }
}
